// 使用moment解析时间
import moment from 'moment'

export type BaseType = "year" | "years" | "y" |
  "month" | "months" | "M" |
  "week" | "weeks" | "w" |
  "day" | "days" | "d" |
  "hour" | "hours" | "h" |
  "minute" | "minutes" | "m" |
  "second" | "seconds" | "s" |
  "millisecond" | "milliseconds" | "ms"

export type AddType = 'years' |
  'months' |
  'weeks' |
  'days' |
  'hours' |
  'minutes' |
  'seconds' |
  'milliseconds' | BaseType

export type WeedDayType = '周' | '星期' | ''

moment.locale('zh-CN',{
  week : {
    dow : 1, // 设置一周的开始为周一
  }
})

export const TimeUtilsDefault = {
  format: 'YYYY-MM-DD HH:mm:ss',
}


export class TimeUtils {

  /**
   * 格式化时间
   * @param time
   * @param format
   */
  static parseTime(time: string | number | Date, format: string = TimeUtilsDefault.format): string {
    return moment(time).format(format)
  }

  /**
   * 格式化时间
   * @param time
   * @param format
   */
  static parseTimeToDate(time: string | number | Date, format: string = TimeUtilsDefault.format): Date {
    return moment(time, format).toDate()
  }

  /**
   * 时间添加
   * @param time
   * @param format
   * @param num
   * @param type
   */
  static add(time: string, format: string = TimeUtilsDefault.format, num: number, type: AddType): string {
    const date = TimeUtils.parseTimeToDate(time, format);
    return TimeUtils.addByDate(date, format, num, type)
  }

  /**
   * 时间添加
   * @param time
   * @param format
   * @param num
   * @param type
   */
  static addByDate(time: Date, format: string = TimeUtilsDefault.format, num: number, type: AddType): string {
    return moment(time).add(num, type).format(format)
  }

  static isBetween(time, start, end, format: string = TimeUtilsDefault.format, type: AddType = 'seconds'): boolean {
    return moment(time, format)
      .isBetween(
        moment(start, format),
        moment(end, format),
        type,
        '[]'
      )
  }

  static isBefore(time, time2, format: string = TimeUtilsDefault.format, type: AddType = 'seconds'): boolean {
    return moment(time, format)
      .isBefore(moment(time2, format), type)
  }

  static isAfter(time, time2, format: string = TimeUtilsDefault.format, type: AddType = 'seconds'): boolean {
    return moment(time, format)
      .isAfter(moment(time2, format), type)
  }

  static getFirstDay(unitOfTime: moment.unitOfTime.StartOf, format: string = TimeUtilsDefault.format): string {
    return moment().startOf(unitOfTime).format(format)
  }

  static getWeekFirstDay(format: string = TimeUtilsDefault.format) {
    return this.getFirstDay('week', format)
  }

  /**
   * 获取星期几
   * @param time
   * @param type
   */
  static getWeekDay(time, type: WeedDayType = '周'): string {
    time = moment(time)
      .day();
    if (!type) {
      return time + ''
    }
    if (time === 1) {
      return type + '一'
    }
    if (time === 2) {
      return type + '二'
    }
    if (time === 3) {
      return type + '三'
    }
    if (time === 4) {
      return type + '四'
    }
    if (time === 5) {
      return type + '五'
    }
    if (time === 6) {
      return type + '六'
    }
    return type + '日'
  }

}
